package com.iweb.servlet;

import com.alibaba.fastjson.JSON;
import com.iweb.entity.ReturnData;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author jay
 * @date 2021/9/13
 * @description 通用的父类sevlet
 */
public class BaseServlet extends HttpServlet {

    /**
     * 将结果格式化成json串输出
     * @param returnData
     * @param resp
     * @throws IOException
     */
    protected void output(ReturnData returnData, HttpServletResponse resp) throws IOException {
        resp.setContentType("text/html;charset=utf-8");
        String jsonString = JSON.toJSONString(returnData);
        PrintWriter writer = resp.getWriter();
        writer.print(jsonString);
        writer.close();
    }
}
